const user = {
  namespaced: true,
  mutations: {
    saveUser(state, address) {
      state.user = address
      this.commit('user/updateAddress')
    },
    updateAddress(state) {
      uni.setStorageSync('user', JSON.stringify(state.user))
    },
    updateUserInfo(state, userinfo) {
      state.userinfo = userinfo;
      this.commit('user/saveUserInfo');
    },
    saveUserInfo(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
    },
    updataToken(state) {
      uni.setStorageSync('token', state.token)
    },
    saveToken(state, token) {
      state.token = token
      this.commit('user/updataToken')
    },
    updataRediectInfo(state, info) {
      state.rediectInfo = info
    }
  },
  state: {
    // 收货地址
    user: JSON.parse(uni.getStorageSync('user') || "{}"),
    // token
    token: uni.getStorageSync('token') || '',
    // 用户信息
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || "{}"),
    // 页面重定向
    rediectInfo: null
  },
  getters: {
    addstr(state) {
      if (!state.user.provinceName) return ''
      return state.user.provinceName + state.user.cityName + state.user.countyName + state.user.detailInfo
    }
  }
}

export default user
